package org.cogaen.java2d;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;

import org.cogaen.core.Core;

/**
 * Utility class for image objects.
 */
public class ImageUtil {

	private static final int DEFAULT_SIZE = 32;
	
	/**
	 *  Transform the specified image to an image that is compatible to the 
	 *  current screen's graphics context.
	 *  
	 *  <p>
	 *  Using an compatible image results in a higher rendering performance.
	 *  </p>
	 *  
	 * @param core reference to {@code Core}, the service locator.
	 * @param image {@code Image} to be converted.
	 * @return converted {@code Image}.
	 */
	public static Image makeCompatibleImage(Core core, BufferedImage image) {
		GraphicsConfiguration gc = SceneManager.getInstance(core).getScreen().getGraphicsConfiguration();
		Image compImage = gc.createCompatibleImage(image.getWidth(), image.getHeight(), image.getTransparency());
		Graphics g = compImage.getGraphics();
		g.drawImage(image, 0, 0, null);
		g.dispose();
		
		return compImage;
	}
	
	/**
	 * Creates a dummy image.
	 * </p>The returned image is compatible to the current screen's graphics 
	 * context.</p>
	 * 
	 * @param core reference to {@code Core}, the service locator.
	 * @return compatible dummy {@code Image}.
	 */
	public static Image createDummyImage(Core core) {
		return createDummyImage(core, DEFAULT_SIZE, DEFAULT_SIZE);
	}
	
	/**
	 * Creates a dummy image with the specified dimension.
	 * </p>The returned image is compatible to the current screen's graphics 
	 * context.</p>
	 * 
	 * @param core reference to {@code Core}, the service locator.
	 * @param width the width of the dummy image to create.
	 * @param height the height of the dummy image to create.
	 * @return compatible dummy {@code Image}.
	 */
	public static Image createDummyImage(Core core, int width, int height) {
		GraphicsConfiguration gc = SceneManager.getInstance(core).getScreen().getGraphicsConfiguration();
		Image compImage = gc.createCompatibleImage(width, height, Transparency.OPAQUE);
		Graphics g = compImage.getGraphics();
		g.setColor(Color.CYAN);
		g.fillRect(0, 0, width, height);
		
		g.setColor(Color.MAGENTA);
		g.drawLine(0,0, width - 1, height - 1);
		g.drawLine(0, height - 1, width - 1, 0);		
		g.drawLine(0, 0, width - 1, 0);
		g.drawLine(width - 1, 0, width - 1, height - 1);
		g.drawLine(0, height - 1, width - 1, height - 1);
		g.drawLine(0, 0, 0, height - 1);
		
		g.dispose();
		
		return compImage;
	}
	
}
